Load/install the following packages:
library("tidyverse")
library("ggridges")
library("rcartocolor")
library("glue")
library("knitr")
theme_set(theme_minimal())
Read in the data
earnings <- read_csv("data/school-earnings.csv")
kable(head(earnings, n = 10))
| School | Gap | Gender | Pay |
|---|---|---|---|
| Berkeley | 17 | Men | 88 |
| Berkeley | 17 | Women | 71 |
| Brown | 20 | Men | 92 |
| Brown | 20 | Women | 72 |
| Chicago | 40 | Men | 118 |
| Chicago | 40 | Women | 78 |
| Columbia | 33 | Men | 119 |
| Columbia | 33 | Women | 86 |
| Cornell | 27 | Men | 107 |
| Cornell | 27 | Women | 80 |
ggplot(earnings, aes(x = Pay, y = School))
ggplot(earnings, aes(x = Pay, y = School)) +
geom_line(size = 1.5, color = "gray70")
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70")
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(aes(color = Gender), size = 4)
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4)
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(aes(label = Gap), color = "gray70")
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2, label = Gap),
color = "gray70")
label.size = NA) and left-align text using hjust (hjust = 0 for left justified, hjust = 0.5 for centered, hjust = 1 for right justified)ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2, label = Gap),
color = "gray70",
hjust = 0,
label.size = NA)
glue package to create the final text annotationggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA)
expand_limits()ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA) +
expand_limits(x = 200)
rcartocolor package (see carto palettes here)ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA) +
expand_limits(x = 200) +
scale_color_carto_d(palette = "Bold", name = NULL)
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA) +
expand_limits(x = 200) +
scale_color_carto_d(palette = "Bold", name = NULL) +
labs(x = "Median Mid-career Salary", y = NULL)
ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA) +
expand_limits(x = 200) +
scale_color_carto_d(palette = "Bold", name = NULL) +
labs(x = "Median Mid-career Salary", y = NULL) +
scale_x_continuous(breaks = c(100, 150, 200),
labels = c("$100k", "$150k", "$200k"))
ggridges package)ggplot(earnings, aes(x = Pay, y = fct_reorder(School, Pay, .fun = "min"))) +
geom_line(size = 1.5, color = "gray70") +
geom_point(size = 6, color = "white") +
geom_point(aes(color = Gender), size = 4) +
geom_label(data = filter(earnings, Gender == "Men"),
aes(x = Pay + 2,
label = glue("Men earn ${Gap}k more than women")),
color = "gray70",
hjust = 0,
label.size = NA) +
expand_limits(x = 200) +
scale_color_carto_d(palette = "Bold", name = NULL) +
labs(x = "Median Mid-career Salary", y = NULL) +
scale_x_continuous(breaks = c(100, 150, 200),
labels = c("$100k", "$150k", "$200k")) +
theme_ridges()